package org.mao.book;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

//诛仙
public class ReadBook3 {

	private Integer basePage = 233734;

	private Integer minPage = 260; // 看第几页

	private Integer perPage = 1; // 每次看几页

	private Integer maxPage = minPage + perPage;

	public static void main(String[] arg) {
		try {
			ReadBook3 readBook = new ReadBook3();
			readBook.load();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void load() throws Exception {
		for (int i = minPage; i < maxPage; i++) {
			System.out.println("====================================第" + i + "页,开始=====================================================================================");
			// http://www.ybdu.com/xiaoshuo/2/2442/233735.html
			read("http://www.ybdu.com/xiaoshuo/2/2442/" + (basePage + i) + ".html");
			System.out.println("====================================第" + i + "页,结束=====================================================================================");
		}
	}

	public void read(String url) throws Exception {
		Document doc = Jsoup.connect(url).get();
		Elements title = doc.getElementsByClass("h1title").select("h1");
		if (title != null) {
			System.out.println(title.text());
		}
		Element ele = doc.getElementById("htmlContent");
		if (ele != null) {
			if (ele.getElementsByClass("ad00") != null) {
				ele.getElementsByClass("ad00").remove();
			}
			if (ele.getElementsByClass("chapter_Turnpage") != null) {
				ele.getElementsByClass("chapter_Turnpage").remove();
			}
			System.out.println(parseContent(ele.html()));
		}
	}

	public String parseContent(String content) {
		content = content.replace("<br>", "");
		content = content.replace("&nbsp;", "");
		return content;
	}

}
